﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoShopping.Model;
using System.Data.Common;
using GoShopping.Model.Enumerations;

namespace GoShopping.BLL.Orders
{
    /// <summary>
    /// 订单处理类
    /// </summary>
    public class OrderBean
    {
        /// <summary>
        /// MD5加密Key
        /// </summary>
        private static readonly string _MD5EncrptKey = "Shopping";


        /// <summary>
        /// 生成一个订单
        /// 订单号Code不用输入，自动生成
        /// </summary>
        /// <param name="prdctList">购买的商品列表</param>
        /// <param name="orderinfo"></param>
        /// <param name="orderCode">返回订单编号</param>
        /// <remarks>返回订单ID</remarks>
        public bool CreateOrder(IList<OrderEachProduct> prdctList, Order orderinfo,out string orderCode)
        {
            bool rt = false;
            DbTransaction trans = Databases.DBGoShopping.BeginTransaction();
            try
            {
                int id = Databases.DBGoShoppingOrder.Select(GoShoppingOrderNBear.Order, GoShoppingOrderNBear.Order.ID.Max()).ToScalar<int>();
                id++;
                orderCode = CreateOrderCode(id);
                Databases.DBGoShoppingOrder.Insert(GoShoppingOrderNBear.Order).AddColumn(
                    GoShoppingOrderNBear.Order.ID, id).AddColumn(
                    GoShoppingOrderNBear.Order.OrderCode, orderCode).AddColumn(
                    GoShoppingOrderNBear.Order.AcceptPeople, orderinfo.AcceptPeople).AddColumn(
                    GoShoppingOrderNBear.Order.AcceptPeopleTel, orderinfo.AcceptPeopleTel).AddColumn(
                    GoShoppingOrderNBear.Order.AcceptType, orderinfo.AcceptType).AddColumn(
                    GoShoppingOrderNBear.Order.City, orderinfo.City).AddColumn(
                    GoShoppingOrderNBear.Order.County, orderinfo.County).AddColumn(
                    GoShoppingOrderNBear.Order.DetailStreet, orderinfo.DetailStreet).AddColumn(
                    GoShoppingOrderNBear.Order.OrderCreateDate, DateTime.Now).AddColumn(
                    GoShoppingOrderNBear.Order.PostPrice, orderinfo.PostPrice).AddColumn(
                    GoShoppingOrderNBear.Order.Province, orderinfo.Province).AddColumn(
                    GoShoppingOrderNBear.Order.State, orderinfo.State).AddColumn(
                    GoShoppingOrderNBear.Order.TotalPrice, orderinfo.TotalPrice).AddColumn(
                    GoShoppingOrderNBear.Order.UserName, orderinfo.UserName).AddColumn(
                    GoShoppingOrderNBear.Order.UserID, orderinfo.UserID).AddColumn(
                    GoShoppingOrderNBear.Order.PayType, orderinfo.PayType).AddColumn(
                    GoShoppingOrderNBear.Order.IsFenceMoney, orderinfo.IsFenceMoney).AddColumn(
                    GoShoppingOrderNBear.Order.FenceMoney, orderinfo.FenceMoney).AddColumn(
                    GoShoppingOrderNBear.Order.NeedIvoice, orderinfo.NeedIvoice).AddColumn(
                    GoShoppingOrderNBear.Order.IvoiceTitle, orderinfo.IvoiceTitle).AddColumn(
                    GoShoppingOrderNBear.Order.IvoiceContent, orderinfo.IvoiceContent).AddColumn(
                    GoShoppingOrderNBear.Order.SendDateRange, orderinfo.SendDateRange).AddColumn(
                    GoShoppingOrderNBear.Order.ZipCode, orderinfo.ZipCode).Execute();
                foreach (OrderEachProduct p in prdctList)
                {
                    Databases.DBGoShoppingOrder.Insert(GoShoppingOrderNBear.OrderEachProduct).AddColumn(
                        GoShoppingOrderNBear.OrderEachProduct.OrderID, id).AddColumn(
                        GoShoppingOrderNBear.OrderEachProduct.ProductID, p.ProductID).AddColumn(
                        GoShoppingOrderNBear.OrderEachProduct.Quantity, p.Quantity).AddColumn(
                        GoShoppingOrderNBear.OrderEachProduct.UnitPrice, p.UnitPrice).Execute();

                    //更新商品数量
                    Databases.DBGoShopping.Update(GoShoppingNBear.Product).AddColumn(
                        GoShoppingNBear.Product.Count, GoShoppingNBear.Product.Count - p.Quantity).Where(
                        GoShoppingNBear.Product.ID == p.ProductID).Execute();
                }


                trans.Commit();
                rt = true;
            }
            catch
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                Databases.DBGoShopping.CloseConnection(trans);
            }
            //orderCode = 
            return rt;
        }

        /// <summary>
        /// 更新一个订单的状态
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="newStatue"></param>
        public void UpdateOrder(int orderID, OrderStatusType newStatue)
        {
            Databases.DBGoShoppingOrder.Update(GoShoppingOrderNBear.OrderStatus).AddColumn(
                GoShoppingOrderNBear.OrderStatus.Status, newStatue.ToString()).Execute();
        }

        /// <summary>
        /// 查询一个订单的状态
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public OrderStatusType SearchOrderStatue(int orderid)
        {
            OrderStatus statue = Databases.DBGoShoppingOrder.Select(
                GoShoppingOrderNBear.OrderStatus).Where(
                GoShoppingOrderNBear.OrderStatus.OrderID == orderid).ToSingleObject<
                OrderStatus>();
            return (OrderStatusType)Enum.Parse(typeof(OrderStatusType), statue.Status);
        }

        /// <summary>
        /// 获取所有订单
        /// </summary>
        /// <returns></returns>
        public IList<Order> GetAllOrders()
        {
            return Databases.DBGoShoppingOrder.Select(GoShoppingOrderNBear.Order).ToList<Order>();
        }

        /// <summary>
        /// 查询一个订单
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public Order GetOneOrder(int orderid)
        {
            return Databases.DBGoShoppingOrder.Select(GoShoppingOrderNBear.Order).Where(
                GoShoppingOrderNBear.Order.ID == orderid).ToSingleObject<Order>();
        }
        /// <summary>
        /// 根据订单编号 查询一个订单
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public Order GetOneOrderByOrderCode(int orderCode)
        {
            return Databases.DBGoShoppingOrder.Select(GoShoppingOrderNBear.Order).Where(
                GoShoppingOrderNBear.Order.OrderCode == orderCode).ToSingleObject<Order>();
        }

        /// <summary>
        /// 获取一个订单的详细情况
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public IList<OrderEachProduct> GetOrderEachPrdct(int orderid)
        {
            return Databases.DBGoShoppingOrder.Select(GoShoppingOrderNBear.OrderEachProduct).Where(
                GoShoppingOrderNBear.OrderEachProduct.OrderID == orderid).ToList<OrderEachProduct>();
        }

        /// <summary>
        /// 查询用户订单
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public IList<Order> GetUsersOrder(string userName)
        {
            return Databases.DBGoShoppingOrder.Select(
                GoShoppingOrderNBear.Order).Where(
                GoShoppingOrderNBear.Order.UserName == userName).ToList<Order>();
        }

        /// <summary>
        /// 查询用户订单
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public IList<Order> GetUsersOrder(int userID)
        {
            return Databases.DBGoShoppingOrder.Select(
                GoShoppingOrderNBear.Order).Where(
                GoShoppingOrderNBear.Order.UserID == userID).ToList<Order>();
        }

        /// <summary>
        /// 记录取消订单
        /// </summary>
        /// <param name="order"></param>
        public bool CancleOrderRecord(CancledOrders order)
        {
            int i = Databases.DBGoShoppingOrder.Insert(GoShoppingOrderNBear.CancledOrders).AddColumn(
                GoShoppingOrderNBear.CancledOrders.CancleDate, DateTime.Now).AddColumn(
                GoShoppingOrderNBear.CancledOrders.OrderID, order.OrderID).AddColumn(
                GoShoppingOrderNBear.CancledOrders.Reason, order.Reason).Execute();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 生成订单号
        /// </summary>
        /// <param name="orderID">订单ID</param>
        /// <returns></returns>
        public string CreateOrderCode(int orderID)
        {
            string s = DateTime.Now.ToString("MMddhhmmss") + orderID.ToString();//orderID.ToString("0#####");
            string week = DateTime.Now.DayOfWeek.ToString().Substring(0, 1);
            string year = DateTime.Now.Year.ToString().Substring(3, 1);
            s = Utility.StringUtil.getSpell(week) + Utility.StringUtil.Reverse(s) + year;
            return s;
        }
        /// <summary>
        /// 根据订单状态获取订单列表
        /// </summary>
        /// <param name="ost"></param>
        /// <returns></returns>
        public IList<Order> GetOrderbyStatus(string[] orderStatus)
        {
            IList<Order> orderList = Databases.DBGoShoppingOrder.Select(
                GoShoppingOrderNBear.Order).Where(
                GoShoppingOrderNBear.Order.State.In(orderStatus)).ToList<Order>();
            return orderList;

        }
    }
}
